//结算页面
package com.example.homework

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.homework.db.UserDBHelper
import com.example.homework.model.User

class ResultActivity : AppCompatActivity() {
    private lateinit var username: String
    private var score = 0

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_result)

        username = intent.getStringExtra("username") ?: ""
        score = intent.getIntExtra("score", 0)

        val dbHelper = UserDBHelper(this)
        val user = dbHelper.login(username)
        dbHelper.close()

        val tvResult = findViewById<TextView>(R.id.tv_result)
        tvResult.text = "$username，你的得分是：$score\n当前最高分：${user.highScore}"

        val btnPlayAgain = findViewById<Button>(R.id.btn_play_again)
        btnPlayAgain.setOnClickListener {
            val intent = Intent(this, GameActivity::class.java).apply {
                putExtra("username", username)
            }
            startActivity(intent)
            finish()
        }

        val btnBack = findViewById<Button>(R.id.btn_back)
        btnBack.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java).apply {
                putExtra("username", username)
                putExtra("highScore", user.highScore)
            }
            startActivity(intent)
            finish()
        }
    }
}